// var url = 'ws://........';//服务器地址
//var url= 'ws://localhost:8080'; //服务器地址
var url = 'ws://r8rejok247.52http.net'; //服务器地址
var req = require('../utils/token.js');


function connect(user, comeFromName, goToName, func) {
  var header = req.CreateHeader();
  wx.connectSocket({
    url: url + comeFromName + goToName ,
    // header: { 'content-type': 'application/json' },
    header: header,
    success: function () {
      console.log('连接成功~')
    },
    fail: function () {
      console.log('连接失败~')
    }

  })

  wx.onSocketOpen(function (res) {

    wx.showToast({

      title: '信道已开通~',

      icon: "success",

      duration: 2000

    })

    //接受服务器消息

    wx.onSocketMessage(func);//func回调可以拿到服务器返回的数据

  });

  wx.onSocketError(function (res) {

    wx.showToast({

      title: '信道连接失败，请检查！',

      icon: "none",

      duration: 2000

    })

  })

}

//发送消息

function send(msg) {

  wx.sendSocketMessage({

    data: msg

  });

}

module.exports = {

  connect: connect,

  send: send

} 